﻿using System;
using System.ComponentModel;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using SD.DataAccess;
using SD.Business;
using SD.Commons.DataFilters;

namespace AB.ServiceDirectory.Design.WebUserControls.Frame
{
    public partial class SupportingMaterialListFrame : UserControl
    {
        #region Properties

        [Category("Manipulation")]
        public string HiddenFieldOrganizationId
        {
            get { return (string)ViewState["HiddenFieldOrganizationId"] ?? string.Empty; }
            set { ViewState["HiddenFieldOrganizationId"] = value; }
        }

        [Category("Manipulation")]
        public int OrganizationId
        {
            get { return ViewState["OrganizationId"] != null ? (int)ViewState["OrganizationId"] : -1; }
            set { ViewState["OrganizationId"] = value; }
        }

        #endregion

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!Page.IsPostBack)
            //{
            SupportingMaterialFilter filter = ViewState["filter"] as SupportingMaterialFilter ??
                                               new SupportingMaterialFilter { OrganizationId = -1 };

            var organisationIdControl = Parent.FindControl(HiddenFieldOrganizationId) as HiddenField;
            int organisationId;
            if (organisationIdControl != null && int.TryParse(organisationIdControl.Value, out organisationId))
            {
                filter.OrganizationId = organisationId;
            }
            else
            {
                filter.OrganizationId = OrganizationId;
            }

            ViewState["filter"] = filter;
            //}
        }

        /// <summary>
        /// Handles the CheckedChanged event of the CheckBoxInactive control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void CheckBoxInactive_CheckedChanged(object sender, EventArgs e)
        {
            var filter = ViewState["filter"] as SupportingMaterialFilter;
            if (filter != null) filter.IncludeInactive = CheckBoxInactive.Checked;
            GridViewSupportingMaterials.DataBind();
        }

        /// <summary>
        /// Objects the data source supporting material selecting.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs"/> instance containing the event data.</param>
        protected void ObjectDataSourceSupportingMaterialSelecting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            var filter = ViewState["filter"] as SupportingMaterialFilter;
            e.InputParameters["filter"] = filter;
        }

        /// <summary>
        /// Handles the Command event of the LinkButtonUrl control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
        protected void LinkButtonUrl_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandArgument.Equals("True"))
            {
                Response.Redirect(string.Format(CultureInfo.CurrentCulture, "~/Orgranization/EditSupportingMaterials.aspx?id={0}",                                                
                                                e.CommandName));
            }
            else
            {
                SD.Business.HOrganizationBO.UpdateStatus(int.Parse(e.CommandName, CultureInfo.CurrentCulture), true);
                Response.Redirect(string.Format(CultureInfo.CurrentCulture, "~/Orgranization/EditSupportingMaterials.aspx?id={0}",                                                
                                                e.CommandName));
            }
        }

        /// <summary>
        /// Handles the Command event of the LinkButtonCreate control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
        protected void LinkButtonCreate_Command(object sender, CommandEventArgs e)
        {
            Response.Redirect(string.Format(CultureInfo.CurrentCulture,
                                                    "~/Orgranization/AddSupportingMaterials.aspx?organisationId={0}",
                                                    OrganizationId));
        }

        /// <summary>
        /// Handles the RowCreated event of the GridViewSupportingMaterials control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewRowEventArgs"/> instance containing the event data.</param>
        protected void GridViewSupportingMaterials_RowCreated(object sender, GridViewRowEventArgs e)
        {
            //var supportingMaterial = e.Row.DataItem as SupportingMaterial;
            //if (supportingMaterial != null && !supportingMaterial.IsActive)
            //{
            //    e.Row.Attributes["onclick"] = "return Confirm_Active();";
            //}
        }

        /// <summary>
        /// Handles the SelectedIndexChanging event of the GridViewSupportingMaterials control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewSelectEventArgs"/> instance containing the event data.</param>
        protected void GridViewSupportingMaterials_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the GridViewSupportingMaterials control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void GridViewSupportingMaterials_SelectedIndexChanged(object sender, EventArgs e)
        {
            var supportingMaterial = GridViewSupportingMaterials.SelectedRow.DataItem as SupportingMaterial;
            if (supportingMaterial != null)
            {
                Response.Write("OK");
            }
        }
    }
}